//
//  RegistViewController.m
//  Experss
//
//  Created by 嗯，大葱 on 17/8/9.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "RegistViewController.h"
#import "NSString+Verify.h"

@interface RegistViewController ()
{
    NSString *sms_codeStr;
}

@property (nonatomic, strong)UIButton *rightBtn2;//右侧按钮

@end

@implementation RegistViewController
-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 0;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    //[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
    [[self.navigationController.navigationBar subviews] objectAtIndex:0].alpha = 1;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configureNaBar];
    _timeLB.hidden = YES;
    _yanZbBtn.hidden = NO;
}
- (void)configureNaBar {
    //设置导航栏按钮的边距
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame= CGRectMake(0, 0, 60, 30);
    [rightBtn setImage:[UIImage imageNamed:@"denglu_cha"] forState:(UIControlStateNormal)];
    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -20;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    //设置导航栏按钮的边距
    _rightBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _rightBtn2.frame= CGRectMake(0, 0, 60, 30);
    [_rightBtn2 setTitle:@"登录" forState:(UIControlStateNormal)];
    [_rightBtn2 addTarget:self action:@selector(handleBackAxtion2) forControlEvents:UIControlEventTouchUpInside];
    _rightBtn2.titleLabel.font = kFont16;
    [_rightBtn2 setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    UIBarButtonItem *btn_right = [[UIBarButtonItem alloc] initWithCustomView:_rightBtn2];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:negativeSpacer, btn_right, nil];
    
}
- (void)handleBackAxtion {
    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)handleBackAxtion2 {
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)yanZenMBtnAction:(UIButton *)sender {
    
    if (![_phoneTF.text verifyTelephone]) {
        
        [self.view showTextNoActivity:@"手机格式不正确" timeLength:1];
        return;
    }
    //获取验证码
    [self getCodeStr];
 
    __block int timeout=59; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
             
                _yanZbBtn.hidden = NO;
                _timeLB.hidden = YES;;
                
            });
        }else{
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2dS", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [UIView beginAnimations:nil context:nil];
                [UIView setAnimationDuration:1];
                _yanZbBtn.hidden = YES;
                _timeLB.hidden = NO;
                _timeLB.text = strTime;
                [UIView commitAnimations];
           
                
            });
            timeout--;
        }
    });
    
    dispatch_resume(_timer);
    

}
-(void)getCodeStr{
    NSDictionary *parameDic = @{@"mobile":_phoneTF.text};
    if ([_phoneTF.text verifyTelephone]) {
        [HttpRequest postWithURLString:POST(Shop_GetSmsCodeUrl) parameters:parameDic success:^(id responseObject) {
            
            NSLog(@"%@",responseObject);
            
            sms_codeStr = [NSString stringWithFormat:@"%@",[[responseObject objectForKey:@"data"] objectForKey:@"sms_code"]];
            
        } failure:^(NSError *error) {
            
        }];
    }
    
}

// 注册按钮的点击事件
- (IBAction)registBtnAction:(UIButton *)sender {
//    [self.navigationController popViewControllerAnimated:YES];
    
    NSDictionary *parameDIc = @{@"mobile":_phoneTF.text,@"password":_passTF.text,@"sms_code":_yanzemTF.text,@"actoken":[Md5Apiway getMD5data:@""]};
    
    BOOL isMoble = [BasePublicMethord isMobileNumber:_phoneTF.text];
    
    
    if (!isMoble) {
        [MBProgressHUD showText:@"请填写正确的手机号!"];
        return;
    }
    
    if (![sms_codeStr isEqualToString:_yanzemTF.text]) {
        [MBProgressHUD showText:@"验证码错误!"];
        return;
    }
    if (!_passTF.text.length) {
        [MBProgressHUD showText:@"请设置密码!"];
        return;
        
    }
    if (![_passTF.text isEqualToString:_passTF2.text])
    {
        [MBProgressHUD showText:@"密码不一致请确认!"];
        return;
    }
    
                
    [HttpRequest postWithURLString:POST(Shop_RegisterUrl) parameters:parameDIc success:^(id responseObject) {
        
        NSLog(@"%@",responseObject);
        
        
        NSDictionary *dataDic = [responseObject objectForKey:@"data"];
        
        if (dataDic.count) {
            NSString *codeStr = [dataDic objectForKey:@"code"];
            
            if (codeStr.integerValue == 0) {
                
                //跳转登录页面
                [self.navigationController popViewControllerAnimated:YES];
            }else{
                [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[dataDic objectForKey:@"msg"]]];
            }
        }else{
            
            [MBProgressHUD showText:[NSString stringWithFormat:@"%@",[responseObject objectForKey:@"msg"]]];
        }
        
        
    } failure:^(NSError *error) {
        
    }];
    
    
    
}



//注册协议
- (IBAction)xieyiAction:(UIButton *)sender {
}

@end
